package com.spsoft.vatoutinv.controller.official;


import cn.hutool.core.date.DateUtil;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.system.model.bo.official.InvoiceDownloadBo;
import com.spsoft.system.model.bo.official.InvoiceQueryBo;
import com.spsoft.system.model.vo.official.InvoiceDownloadVO;
import com.spsoft.system.model.vo.official.InvoiceQueryVO;
import com.spsoft.vatoutinv.service.official.OfficialInvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 官网接口
 * 查询开票交易信息
 * 触发流水开票接口
 */
@Api(tags = {"官网接口"})
@RequiredArgsConstructor
@Validated
@Slf4j
@RestController
@RequestMapping("/official/invoice/")
public class OfficialInvoiceController {

    private final OfficialInvoiceService officialInvoiceService;

    @ApiOperation("官网接口-查询开票交易信息")
    @AnonymousAccess(value = "/invoiceQuery", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "官网接口-查询开票交易信息", type = LogActionType.SELECT, menuName = LogMenuEnum.OFFICIAL_INTERFACE)
    public Result<List<InvoiceQueryVO>> invoiceQuery(@RequestBody @Validated InvoiceQueryBo dto) {
        log.info("官网接口-查询开票交易信息，入参：{}", dto);
        // 校验查询日期 二者要么都存在要么都不存在
        if (StringUtil.isEmpty(dto.getQsrq()) && StringUtil.isNotBlank(dto.getJzrq())) {
            return Result.ERRORT(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "起始日期或截止日期同时存在或都不填。");
        }
        if (StringUtil.isEmpty(dto.getJzrq()) && StringUtil.isNotBlank(dto.getQsrq())) {
            return Result.ERRORT(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "起始日期或截止日期同时存在或都不填。");
        }

        if (StringUtil.isNotBlank(dto.getQsrq()) && StringUtil.isNotBlank(dto.getJzrq())) {
            if (dto.getQsrq().length() != 8 || dto.getJzrq().length() != 8) {
                return Result.ERRORT(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "起始日期或截止日期，格式错误。");
            }
            try {
                Date qsrq = DateUtil.parse(dto.getQsrq(),"yyyyMMdd");
                Date jzrq = DateUtil.parse(dto.getJzrq(),"yyyyMMdd");
            } catch (Exception e) {
                return Result.ERRORT(ResponseEnum.PARAM_VALIDATED_ERROR.getCode(), "起始日期或截止日期，格式错误。");
            }
        }

        List<InvoiceQueryVO> list = officialInvoiceService.invoiceQuery(dto);
        if (list == null) {
            return Result.ERROR(ResponseEnum.OFFICIAL_DZFPPT_NOFLOW_ERROR);
        }
        return Result.OK(list);
    }

    @ApiOperation("官网接口-开票")
    @AnonymousAccess(value = "/invoiceDownload", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "官网接口-开票", type = LogActionType.UPDATE, menuName = LogMenuEnum.OFFICIAL_INTERFACE)
    public Result<InvoiceDownloadVO> invoiceDownload(@RequestBody @Validated InvoiceDownloadBo bo) {
        log.info("官网开票，入参：{}", bo);
        try {
            Date jyrq = DateUtil.parse(bo.getJyrq(),"yyyyMMdd");
        } catch (Exception e) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        return officialInvoiceService.invoiceDownload(bo);
    }

}
